/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package east2west.action;

import east2west.dao.FlightDAO;
import east2west.dto.FlightsDTO;
import east2west.service.FlightsService;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.validator.DynaValidatorForm;

/**
 *
 * @author Admin
 */
public class AdminFlightAction extends DispatchAction {

    FlightsService flighser = null;
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String ERROR = "err";
    private static final String TRUE = "true";

  
    public ActionForward listFlight(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        FlightDAO dao = new FlightDAO();
        ArrayList<FlightsDTO> list = dao.readByAll();
        request.setAttribute("list", list);
        request.setAttribute("page", "flight.page");
        return mapping.findForward(SUCCESS);
    }

    public ActionForward insertFlight(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        FlightsDTO dto = new FlightsDTO();
        FlightDAO dao = new FlightDAO();
        dto.setFlightCode(request.getParameter("frflightcode"));
        dto.setDeparture(request.getParameter("frflightdepar"));
        dto.setDestination(request.getParameter("frflightdes"));
        dto.setFlightDate((request.getParameter("frflightdate")));
        dto.setFlightTime(request.getParameter("frflighttime"));
        dto.setWebSite(request.getParameter("frflightweb"));
        dto.setEmail(request.getParameter("frflightemail").trim());
        dto.setDescription(request.getParameter("frflightdess"));
        if (dao.create(dto)) {
            return mapping.findForward(TRUE);
        }
        else
        {
            return mapping.findForward(ERROR);
        }
        
    }

    public ActionForward deleteFlight(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int id = Integer.parseInt(request.getParameter("frflightidd"));
        FlightDAO dao = new FlightDAO();
        if (dao.delete(id)) {
            return mapping.findForward(TRUE);
        }
        else
        {
            return mapping.findForward(ERROR);
        }
    }

    public ActionForward UpdateFlight(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // int id = Integer.parseInt(request.getParameter("id"));
        FlightsDTO dto = new FlightsDTO();
        FlightDAO dao = new FlightDAO();
        dto.setFlightId(Integer.parseInt(request.getParameter("frflightidd")));
        dto.setFlightCode(request.getParameter("frflightcode"));
        dto.setDeparture(request.getParameter("frflightdepar"));
        dto.setDestination(request.getParameter("frflightdes"));
        dto.setFlightDate((request.getParameter("frflightdate")));
        dto.setFlightTime(request.getParameter("frflighttime"));
        dto.setWebSite(request.getParameter("frflightweb"));
        dto.setEmail(request.getParameter("frflightemail").trim());
        dto.setDescription(request.getParameter("frflightdess"));
        if (dao.update(dto)) {
            return mapping.findForward(TRUE);
        }
         else
        {
            return mapping.findForward(ERROR);
        }
    }

    public ActionForward listeditFlight(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int id = Integer.parseInt(request.getParameter("id"));
        FlightDAO dao = new FlightDAO();
        FlightsDTO dto = dao.findById(id);
        request.setAttribute("flight", dto);
        DynaValidatorForm f = (DynaValidatorForm) form;
        BeanUtils.copyProperties(f,dto);
        request.setAttribute("page", "editFlight.page");
        return mapping.findForward(SUCCESS);
    }
}
